﻿using System;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;

namespace EasyLibrary.AR.Classes
{
    public class Utilizador : ActiveRecord
    {
        private string login;
        private string nome;
        private string password;


        public Utilizador()
        {
            Idutilizador = -1;
            Refid_pais = -1;
            nome = string.Empty;
            Email = string.Empty;
            login = string.Empty;
            password = string.Empty;
            Administrador = true;
            Activo = true;
            Datainsercao = DateTime.Now;
            Myutilizador = null;
            Mypais = null;
        }


        public int Idutilizador { get; set; }

        public string Nome
        {
            get { return nome; }
            set { nome = value; }
        }

        public string Email { get; set; }

        public string _Login
        {
            get { return login; }
            set { login = value; }
        }

        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        public bool Administrador { get; set; }

        public bool Activo { get; set; }

        public DateTime Datainsercao { get; set; }

        public Utilizador Myutilizador { get; set; }

        public Pais Mypais { get; set; }

        public int Refid_pais { get; set; }


        public DataSet Login(string UserName, string pw, string browserT, string browserV, NameValueCollection HttpAgent)
        {
            var u = new Utilizador();
            u.login = UserName;
            u.password = pw;

            SqlConnection cnx = GetConnection(false);
            var sqlcomm = new SqlCommand("Autenticacao", cnx);
            sqlcomm.CommandType = CommandType.StoredProcedure;
            sqlcomm.Parameters.Add(new SqlParameter("@Login", SqlDbType.VarChar, 50));
            sqlcomm.Parameters.Add(new SqlParameter("@Password", SqlDbType.VarChar, 50));
            sqlcomm.Parameters.Add(new SqlParameter("@BrowserType", SqlDbType.VarChar, 50));
            sqlcomm.Parameters.Add(new SqlParameter("@BrowserVersion", SqlDbType.VarChar, 50));
            sqlcomm.Parameters.Add(new SqlParameter("@HTTP_USER_AGENT", SqlDbType.VarChar, 50));
           // sqlcomm.Parameters.Add(new SqlParameter("@IDUtilizadores", SqlDbType.Int));

            sqlcomm.Parameters["@Login"].Value = u.nome;
            sqlcomm.Parameters["@Password"].Value = u.password;
            sqlcomm.Parameters["@BrowserType"].Value = browserT;
            sqlcomm.Parameters["@BrowserVersion"].Value = browserV;
            sqlcomm.Parameters["@HTTP_USER_AGENT"].Value = HttpAgent.ToString();
           // sqlcomm.Parameters["@IDUtilizadores"].Direction = ParameterDirection.Output;
     
        
            DataSet myDataSet=new DataSet();
           // myDataSet = executeCommandOnDataSet("EXEC Autenticacao ' " + sqlcomm.Parameters["@Login"].Value + "', '" + sqlcomm.Parameters["@Password"].Value + "', '" + sqlcomm.Parameters["@BrowserType"].Value + "','" + sqlcomm.Parameters["@BrowserVersion"].Value + "'," + "'" + sqlcomm.Parameters["@HTTP_USER_AGENT"].Value + "'");

            myDataSet = executeCommandOnDataSet("EXEC Autenticacao '" + u.login + "', '" + u.password + "', '" + browserT + "','" + browserV + "'," + "'" + HttpAgent + "'");

            if (myDataSet.Tables[0].Rows[0][0].ToString().Length > 0)
            {
               
                return myDataSet;
            }

            return null;
        }


        public override void Save()
        {
//            throw new NotImplementedException();
        }
    }
}